package com.example.demo.myUtils;

import com.alibaba.fastjson.JSON;
import com.example.demo.pojo.ResultPojo;
import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * @Description:
 * @Auther:ccc
 * @Date:2023/2/25 15:38
 */
public class WebUtils {
    /**
     * 返回json数据
     */
    public static void writeJson(HttpServletResponse response, ResultPojo resultPojo) {
        response.setHeader("Content-Type", "application/json;charset=UTF-8");
        //response.setContentType("application/json;charset=UTF-8");
        response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        PrintWriter out = null;
        try {
            String data = JSON.toJSONString(resultPojo);
            out = response.getWriter();
            out.print(data);
            out.flush();
        } catch (Exception e) {

        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
}
